<?php
/**
 * Created by phpstorm.
 * User: WML
 * Date: 2019/9/9 0009
 * Time: 14:08
 */

namespace app\chat\controller;


use think\Controller;
use think\Db;
use think\Exception;
use think\facade\Session;

class Login extends Controller
{
//    public function login()
//    {
//        $input = input();
//        if(!empty($input)){
//            dump($input['msg']);
//        }
//        return view("login");
//    }
    public function checkLogin()
    {
        if (request()->post()) {
            $param = request()->param();
            $user  = db("users")->where('username', $param['username'])->find();
            if (empty($user) || !$user) {
                $this->redirect('Login/login', ['code' => -1, 'msg' => '用户不存在']);
            }
            if ($param['password'] != $user['password']) {
                $this->redirect('Login/login', ['code' => -2, 'msg' => '密码不正确']);
            }
            Session::set('chat_user_id', $user['id']);
            $this->success('登录成功', 'User/chatList');
        }
    }

    public function checkRegister()
    {
        if (request()->post()) {
            $param = request()->param();
//            dump($param);
            $user = db("users")->where('username', $param['username'])->find();
            if ($user) {
                $this->redirect('Login/login', ['code' => -1, 'msg' => '该用户已存在']);
            }
            if ($param['password'] != $param['repassword']) {
                $this->redirect('Login/login', ['code' => -1, 'msg' => '两次密码不一致']);
            }
            Db::startTrans();
            try {
                $data      = [
                    'username' => $param['username'],
                    'password' => $param['password']
                ];
                $insertId  = db("users")->insertGetId($data);
                $data_info = [
                    'nick_name' => '用户' . $insertId,
                    'head_img'  => randHeadImage()
                ];
                db("users")->where('id', $insertId)->update($data_info);
                Db::commit();
                return $this->redirect('User/chatList');
            } catch (\Exception $e) {
                Db::rollback();
                return $this->redirect('Login/login');
            }
        }
    }

    public function checkUsername()
    {
        if (request()->isAjax()) {
            $username = input("username");
            $user     = db("users")->where('username', $username)->find();
            if (!empty($user)) {
                $this->redirect('Login/login', ['code' => -3, 'msg' => '该账号已经存在']);
            }
        }
    }
    /*********** 以上代码废弃 ***********/
    /**
     * 登录
     * @return \think\response\View
     * @throws Exception
     */
    public function login()
    {
        if (request()->post()) {
            $param = request()->param();
            $user  = db("users")->where('account', $param['uname'])->whereOr('mobile', $param['uname'])->find();
            if (empty($user) || !$user) {
                $this->error('用户不存在', 'Login/login');
            }
            if ($param['upwd'] != $user['password']) {
                $this->error('密码不正确', 'Login/login');
            }
            Session::set('chat_user_id', $user['id']);
            $this->success('登录成功', 'Index/index');
        }
        return view("index");
    }

    /**
     * 注册
     * @return \think\response\View
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function register()
    {
        if (request()->post()) {
            $param = request()->param();
            if (empty($param['uname']) || empty($param['upwd']) || empty($param['upwd_2']) || empty($param['phone'])) {
                $this->error('输入不完成整', 'Login/register');
            }
            $user = db("users")->where('account', $param['uname'])->whereOr('mobile', $param['uname'])->find();
            if (!empty($user)) {
                $this->error('该用户已经存在', 'Login/register');
            }
            if ($param['upwd_2'] != $param['upwd']) {
                $this->error('两次密码不一致', 'Login/register');
            }
            $data      = [
                'account'  => $param['uname'],
                'password' => $param['upwd'],
                'mobile'   => $param['phone']
            ];
            $insertId  = db("users")->insertGetId($data);
            $data_info = [
                'nick_name' => '用户' . $insertId,
                'head_img'  => randHeadImage()
            ];
            $update    = db("users")->where('id', $insertId)->update($data_info);
            if ($insertId && $update) {
                $this->success('注册成功,请先登录', 'Login/login');
            } else {
                $this->error('注册失败，请刷新后重试', 'Login/register');
            }
        }
        return view("register");
    }
}